/**
 * AuthService.java
 * Version 1.0.0
 * Created on 2018年7月11日
 */
package com.yincheng.oes.sso.service;

import com.yincheng.oes.sso.entity.MenuAuth;
import com.yincheng.oes.sso.entity.RoleMenuAuth;
import com.yincheng.oes.sso.entity.UserRole;
import com.yincheng.oes.sso.mapper.MenuAuthMapper;
import com.yincheng.oes.sso.mapper.RoleMenuAuthMapper;
import com.yincheng.oes.sso.mapper.UserRoleMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yangsy
 * @version 1.0.0
 * @since
 * 创建时间：2018年7月11日
 * 功能描述：用户权限服务类
 * 邮箱地址：446948434@qq.com
 */
@Service
public class AuthService {

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private MenuAuthMapper menuAuthMapper;

    @Autowired
    private RoleMenuAuthMapper roleMenuAuthMapper;

    /**
     * 根据用户id获取用户所有的菜单权限列表
     *
     * @param userId
     * @return
     */
    public List<MenuAuth> getMenuAuthList(String userId) {
        List<MenuAuth> menuAuthList = new ArrayList<MenuAuth>();
        List<UserRole> userRoleList = (List<UserRole>) CollectionUtils
                .emptyIfNull(userRoleMapper.selectByUserId(userId));
        for (UserRole userRole : userRoleList) {
            String roleId = userRole.getRoleId();
            List<RoleMenuAuth> roleMenuAuthList = (List<RoleMenuAuth>) CollectionUtils
                    .emptyIfNull(roleMenuAuthMapper.selectByRoleId(roleId));
            for (RoleMenuAuth roleMenuAuth : roleMenuAuthList) {
                Integer menuId = roleMenuAuth.getMenuId();
                MenuAuth menuAuth = menuAuthMapper.selectByMenuId(menuId);
                menuAuthList.add(menuAuth);
            }
        }
        menuAuthList.sort((c1, c2) -> c1.getOrderNum().compareTo(c2.getOrderNum()));
        return menuAuthList;
    }
}
